.org 0xE68D0 ; Terrain Firepower

Skills_Terrain_Firepower:

	PUSH 	{R4-R8, LR}
	
	LDR 	R5, [Terrain_Firepower_1]
	LDR 	R4, [Terrain_Firepower_2]
	LDR 	R5, [R5, 0]
	LDR 	R4, [R4, 0]
	MOV 	R7, R1
	ADD 	R1, R5, R4, LSL 2
	LDR 	R1, [R1, 0x48]
	MOV 	R8, R0
	ADD 	R1, R1, 0x4000
	LDR 	R4, [R1, 0x3D0]
	ADD 	R1, R4, R3, LSL 1
	ADD 	R1, R1, 0x4100
	LDRH 	R1, [R1, 0X68]
	ADD 	R1, R2, R1
	ADD 	R1, R4, R1
	ADD 	R1, R1, 0x1000
	
	LDRB 	R1, [R1, 0x420]
	
	AND 	R6, R1, 0x1F
	MOV 	R1, R6
	BL 		Terrain_Firepower_Sub
	
;-------------------------------------------------------------------------------	
	
	LDR 	R3, [Terrain_Firepower_1]
	LDR 	R2, [Terrain_Firepower_2]
	MOV 	R1, 0x98
	LDR 	R3, [R3, 0]
	LDR 	R2, [R2, 0]
	MUL 	R4, R8, R1
	ADD 	R1, R3, R2, LSL 2
	LDR 	R1, [R1, 0x48]
	ADD 	R5, R0, 0
	ADD 	R0, R1, 0x5000
	LDR 	R0, [R0, 0xA58]
	
	MOV 	R1, CO_Terrain_Firepower			; Skill: Terrain to Firepower
	
	ADD 	R0, R0, R4
	ADD 	R0, R0, 0x74
	
	BL 		Check_Skills_Equipped				; Check the Skill is equipped
	
	CMP 	R0, 0								; Does CO use that skill?
	BEQ 	No_Star_Terrain_Firepower			; Skip if CO isn't using that skill
	MOV 	R0, R8
	MOV 	R1, R7	
	MOV 	R2, R6
	
	BL 		Double_Terrain_Stars
	
	ADD 	R5, R5, R0, ASR 1					; Add the extra firepower per terrain star (+5%)

;-------------------------------------------------------------------------------
	
No_Star_Terrain_Firepower:
	
	CMP 	R6, 7								; Number of pointers to check
	ADDLS 	PC, PC, R6, LSL #2 					; Pointer used depends on type of terrain the unit is on
	
	B 		No_Terrain_Rank
	B 		No_Terrain_Rank 					;  
	B 		No_Terrain_Rank 	 				; Plain
	B 		No_Terrain_Rank	 					; River
	B 		Mountaineer_Rank					; Mountain
	B 		Ranger_Rank							; Woods
	B 		Road_Rage_Rank						; Road
	B 		Urban_Fighter_Rank					; City
	B 		Seamanship_Rank						; Sea	

;-------------------------------------------------------------------------------	
	
Seamanship_Rank:
	
	LDR 	R1, [Terrain_Firepower_1]
	LDR 	R0, [Terrain_Firepower_2]
	LDR 	R2, [R1, 0]
	LDR 	R0, [R0, 0]
	
	MOV 	R1, Rank_Seamanship					; Rank: Seamanship
	
	ADD 	R0, R2, R0, LSL 2
	LDR 	R0, [R0, 0x48]
	ADD 	R0, R0, 0x5000
	LDR 	R0, [R0, 0xA58]
	ADD 	R0, R0, R4
	ADD 	R0, R0, 0x74
	
	BL 		Check_Skills_Equipped				; Check the Skill is equipped
	
	CMP 	R0, 0								; Does CO use that skill?
	ADDNE 	R5, R5, 10							; If CO use skill, add +10 firepower
	
	B 		No_Terrain_Rank
	
;-------------------------------------------------------------------------------	

Road_Rage_Rank:

	LDR 	R1, [Terrain_Firepower_1]
	LDR 	R0, [Terrain_Firepower_2]
	LDR 	R2, [R1, 0]
	LDR 	R0, [R0, 0]
	
	MOV 	R1, Rank_Road_Rage					; Rank: Road Rage
	
	ADD 	R0, R2, R0, LSL 2
	LDR 	R0, [R0, 0x48]
	ADD 	R0, R0, 0x5000
	LDR 	R0, [R0, 0xA58]
	ADD 	R0, R0, R4
	ADD 	R0, R0, 0x74
	
	BL 		Check_Skills_Equipped				; Check the Skill is equipped
	
	CMP 	R0, 0								; Does CO use that skill?
	ADDNE 	R5, R5, 10							; If CO use skill, add +10 firepower
	
	B 		No_Terrain_Rank

;-------------------------------------------------------------------------------	

Urban_Fighter_Rank:	

	LDR 	R1, [Terrain_Firepower_1]
	LDR 	R0, [Terrain_Firepower_2]
	LDR 	R2, [R1, 0]
	LDR 	R0, [R0, 0]
	
	MOV 	R1, Rank_Urban_Fighter				; Rank: Urban Fighter
	
	ADD 	R0, R2, R0, LSL 2
	LDR 	R0, [R0, 0x48]
	ADD 	R0, R0, 0x5000
	LDR 	R0, [R0, 0xA58]
	ADD 	R0, R0, R4
	ADD 	R0, R0, 0x74
	
	BL 		Check_Skills_Equipped				; Check the Skill is equipped
	
	CMP 	R0, 0								; Does CO use that skill?
	ADDNE 	R5, R5, 10							; If CO use skill, add +10 firepower
	
	B 		No_Terrain_Rank

;-------------------------------------------------------------------------------	

Mountaineer_Rank:

	LDR 	R1, [Terrain_Firepower_1]
	LDR 	R0, [Terrain_Firepower_2]
	LDR 	R2, [R1, 0]
	LDR 	R0, [R0, 0]
	
	MOV 	R1, Rank_Mountaineer				; Rank: Mountaineer
	
	ADD 	R0, R2, R0, LSL 2
	LDR 	R0, [R0, 0x48]
	ADD 	R0, R0, 0x5000
	LDR 	R0, [R0, 0xA58]
	ADD 	R0, R0, R4
	ADD 	R0, R0, 0x74
	
	BL 		Check_Skills_Equipped				; Check the Skill is equipped
	
	CMP 	R0, 0								; Does CO use that skill?
	ADDNE 	R5, R5, 10							; If CO use skill, add +10 firepower
	
	B 		No_Terrain_Rank

;-------------------------------------------------------------------------------	

Ranger_Rank:

	LDR 	R1, [Terrain_Firepower_1]
	LDR 	R0, [Terrain_Firepower_2]
	LDR 	R2, [R1, 0]
	LDR 	R0, [R0, 0]
	
	MOV 	R1, Rank_Ranger						; Rank: Ranger
	
	ADD 	R0, R2, R0, LSL 2
	LDR 	R0, [R0, 0x48]
	ADD 	R0, R0, 0x5000
	LDR 	R0, [R0, 0xA58]
	ADD 	R0, R0, R4
	ADD 	R0, R0, 0x74
	
	BL 		Check_Skills_Equipped				; Check the Skill is equipped
	
	CMP 	R0, 0								; Does CO use that skill?
	ADDNE 	R5, R5, 10							; If CO use skill, add +10 firepower
	
;-------------------------------------------------------------------------------	
	
No_Terrain_Rank:

	MOV 	R0, R5
	POP 	{R4-R8, LR}
	
	BX 		LR
		
;-------------------------------------------------------------------------------

Terrain_Firepower_1:
	.dw Reg1_Skill
	
Terrain_Firepower_2:
	.dw Reg2_Skill
	
;-------------------------------------------------------------------------------	
	
.org 0xDFCF8
Terrain_Firepower_Sub:	



